//
//  XCInTheRadioViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/16.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCInTheRadioViewController.h"
#import "XCInTheRadioDataModel.h"
#import "OnlineAVplay.h"
#import "UIImageView+WebCache.h"
#import "XCPlayViewController.h"
#import "MBProgressHUD+NJ.h"
@interface XCInTheRadioViewController ()

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSString *picUrl;

@property (nonatomic, retain) NSNumber *tag_id;

@property (nonatomic, retain) UIImageView *inTheRadioImageView;

@property (nonatomic, copy) NSString *navigationName;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;

@end

@implementation XCInTheRadioViewController

- (instancetype)initWithTag:(NSNumber *)tag_id WithTagName:(NSString *)tag_name
{
    self = [super init];
    if (self) {
        self.tag_id = tag_id;
        self.navigationName = tag_name;
    }
    return self;
}

- (void)dealloc {
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_dataSource release];
    [_picUrl release];
    [_tableView release];
    [_tag_id release];
    [_inTheRadioImageView release];
    [_mutableArraySongName release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.navigationName;
    [self InTheRadioNetRequest];
    [self inTheRadioTableViewBuild];
    
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
}
- (void)inTheRadioTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.inTheRadioImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    [view addSubview:self.inTheRadioImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [view release];
    [_tableView release];
    [_inTheRadioImageView release];
}

- (void)InTheRadioNetRequest {
    
    [XCAFNetTool GETNetWithUrl:[XCUrlManger inTheRadioNetRequest:self.tag_id] body:nil headerFile:nil response:XCJSON success:^(id result) {
        
        [MBProgressHUD showMessage:@"正在加载"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
        
        
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in result[@"data"]) {
            XCInTheRadioDataModel *radioDataModel = [[XCInTheRadioDataModel alloc] initWithDataSource:dic];
            
            if (radioDataModel.url_list.count != 0 ) {
                [self.dataSource addObject:radioDataModel];
            }
            [radioDataModel release];

        }
        
        
        for (XCInTheRadioDataModel *inTheRadioDataModel in self.dataSource) {

                [self.mutableArraySongName addObject:[inTheRadioDataModel.url_list[0] objectForKey:@"url"]];
                [self.mutableArraySingerNAme addObject:inTheRadioDataModel.singer_name];
                [self.nameArray addObject:inTheRadioDataModel.song_name];
                if ([inTheRadioDataModel.mv_list[0]objectForKey:@"picUrl"]) {
                    [self.mutableHeadIVPicurl addObject:[inTheRadioDataModel.mv_list[0]objectForKey:@"picUrl"]];
                } else {
                    [self.mutableHeadIVPicurl addObject:@"http://3p.pic.ttdtweb.com/3p.ttpod.com/singer/2265534/7063063.jpg"];
                }

        }

        XCInTheRadioDataModel *radioDataModel = self.dataSource[arc4random() % 10 + 1];
        [self.inTheRadioImageView sd_setImageWithURL:[NSURL URLWithString:[radioDataModel.mv_list[0] objectForKey:@"picUrl"]]placeholderImage:[UIImage imageNamed:@"Image-p"]];

        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
    }];
 
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.inTheRadioDataModel = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    
    XCInTheRadioDataModel *inTheRadioDataModel = self.dataSource[indexPath.row];
    [[OnlineAVplay shareAVPlayer] playWithUrl:[inTheRadioDataModel.url_list[0] objectForKey:@"url"]];
    
    [[OnlineAVplay shareAVPlayer] play];
    [OnlineAVplay shareAVPlayer].isPlay = YES;

    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
    
    
   [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.inTheRadioImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.inTheRadioImageView.transform = CGAffineTransformMakeScale(scale, scale);
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return high / 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
